<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once "class/Load.class.php";
require_once "class/WowTable.class.php";
require_once "class/ListTable.class.php";
require_once "class/Tooltip.class.php";
require_once "class/Config.class.php";
require_once "class/Inputs.class.php";
require_once "class/StatusRoster.class.php";
require_once "class/SessionRoster.class.php";
require_once "class/Localization.class.php";
require_once "class/CharacterManager.class.php";
require_once "class/LocaleContainer.class.php";
require_once "class/CharacterDisplayer.class.php";
require_once "class/SubscriptionManager.class.php";
require_once "class/TradeskillContainer.class.php";
require_once "class/TradeskillSpecializationContainer.class.php";

$load = Load::GetInstance();
$inputs = Inputs::GetInstance();
$config = Config::GetInstance();
$localization = Localization::GetInstance();
$status_roster = StatusRoster::GetInstance();
$session_roster = SessionRoster::GetInstance();
$subscription_manager = SubscriptionManager::GetInstance();
$character_displayer = CharacterDisplayer::GetInstance();
$tooltip = Tooltip::GetInstance();
$locale_container = LocaleContainer::GetInstance();

if ($session_roster->IsConnected() == false)
{
	$session_roster->Redirect("connection.php");
}

if ( $inputs->Post('c_password') )
{
	if ( $subscription_manager->IsAccountValid($subscription_manager->GetSubscription($session_roster->GetAccountId())->GetName(), $inputs->Post('oldpass')) == false )
	{
		$status_roster->AddError( $localization->Get('invalid password') );
	}
	elseif ( $inputs->Post('newpass') != $inputs->Post('confirmpass') )
	{
		$status_roster->AddError( $localization->Get('the passwords do not match') );
	}
	elseif ( $subscription_manager->CheckPassword($inputs->Post('newpass')) == false )
	{
		$status_roster->AddError( $localization->Get('your password is too short (%s characters min)', $subscription_manager->GetPasswordMinimumSize()) );
	}
	elseif( $subscription_manager->UpdatePassword( $session_roster->GetAccountId(), $inputs->Post('newpass') ) == false )
	{
		$status_roster->AddError( $localization->Get('could not update your password') );
	}
	else
	{
		$status_roster->AddSuccess( $localization->Get('password updated successfully') );
		$status_roster->AddNote( $localization->Get('use this password the next time you (...)') );
	}
	
	$session_roster->SaveStatus( $status_roster );
	$session_roster->Redirect("preferences.php");
}

if ( $inputs->HasGetData('c_del') )
{	
	$character_manager = CharacterManager::GetInstance();
	
	foreach ($subscription_manager->GetSubscription( $session_roster->GetAccountId() )->GetOwnedCharactersWithTradeskills() as $character)
	{
		if ( $character->GetId() == $inputs->Get('id') )
		{
			if ($character_manager->DeleteCharacter($character->GetName()) == false)
			{
				$status_roster->AddError($localization->Get('character deletion failed'));
			}
			else
			{
				$status_roster->AddSuccess($localization->Get('character successfully delete'));
			}
			
			$session_roster->SaveStatus($status_roster);
			$session_roster->Redirect("preferences.php");
		}
	}
}

if ( $inputs->HasGetData('t_del') )
{	
	$character_manager = CharacterManager::GetInstance();
	$tradeskill_container = TradeskillContainer::GetInstance();
	
	foreach ($subscription_manager->GetSubscription( $session_roster->GetAccountId() )->GetOwnedCharactersWithTradeskills() as $character)
	{
		if ( $character->GetId() == $inputs->Get('id') )
		{
			if ($tradeskill_container->HasTradeskillId( $inputs->Get('t') ) == false)
			{
				$status_roster->AddError($localization->Get('this tradeskill does not exist'));
			}
			elseif ($character_manager->DeleteCharacterTradeskills($character->GetName(), $inputs->Get('t')) == false)
			{
				$status_roster->AddError($localization->Get('tradeskills deletion failed'));
			}
			else
			{
				$status_roster->AddSuccess($localization->Get('tradeskills sucessfully deleted'));
			}
			
			$session_roster->SaveStatus($status_roster);
			$session_roster->Redirect("preferences.php");
		}
	}
}

if ( $inputs->HasGetData('s_del') )
{	
	$character_manager = CharacterManager::GetInstance();
	$tradeskill_specialization_container = TradeskillSpecializationContainer::GetInstance();
	
	foreach ($subscription_manager->GetSubscription( $session_roster->GetAccountId() )->GetOwnedCharactersWithTradeskills() as $character)
	{
		if ( $character->GetId() == $inputs->Get('id') )
		{
			if ($tradeskill_specialization_container->HasTradeskillSpecializationId( $inputs->Get('s') ) == false)
			{
				$status_roster->AddError($localization->Get('this tradeskill specialization does not exist'));
			}
			elseif ($character_manager->DeleteCharacterTradeskillSpecialization($character->GetName(), $inputs->Get('s')) == false)
			{
				$status_roster->AddError($localization->Get('tradeskill specialization deletion failed'));
			}
			else
			{
				$status_roster->AddSuccess($localization->Get('tradeskill specialization sucessfully deleted'));
			}
			
			$session_roster->SaveStatus($status_roster);
			$session_roster->Redirect("preferences.php");
		}
	}
}

if ( $inputs->HasPostData('del_account') )
{
	if ( $subscription_manager->IsAccountValid($subscription_manager->GetSubscription($session_roster->GetAccountId())->GetName(), $inputs->Post('pass')) == false )
	{
		$status_roster->AddError( $localization->Get('invalid password') );
	}
	elseif ( $subscription_manager->Delete( $session_roster->GetAccountId() ) == false )
	{
		$status_roster->AddError( $localization->Get('could not delete account') );
	}
	else
	{
		$session_roster->Redirect("disconnection.php?acc_del");
	}
	
	$session_roster->SaveStatus($status_roster);
	$session_roster->Redirect("preferences.php");
}

$wow_table = new WowTable($localization->Get('your characters'));
$wow_table->AddSubsection();
$wow_table->AddColumn( $localization->Get('character') );
$wow_table->AddColumn( $localization->Get('class'), "center", 45 );
$wow_table->AddColumn( $localization->Get('remove a specialization'), "center", 120 );
$wow_table->AddColumn( $localization->Get('remove a tradeskill'), null, 400 );
$wow_table->AddColumn( $localization->Get('delete'), "center" );

foreach ($subscription_manager->GetSubscription( $session_roster->GetAccountId() )->GetOwnedCharactersWithTradeskills() as $character)
{
	$wow_table->AddLine(0, array(
		$character_displayer->Name($character),
		$character_displayer->Class_($character),
		$character_displayer->__DeleteSpecialization($character),
		$character_displayer->__DeleteTradeskill($character),
		$character_displayer->__Delete($character),
	));
}

$list_table = new ListTable($localization->Get('your statistics'));
$list_table->AddLine($localization->Get('number of times you have uploaded'), $subscription_manager->GetSubscription( $session_roster->GetAccountId() )->GetTimesUploaded());
$list_table->AddLine($localization->Get('number of items updated'), $subscription_manager->GetSubscription( $session_roster->GetAccountId() )->GetNbItemsUpdated().$localization->Get('item(s)'));
$list_table->AddLine($localization->Get('number of spells updated'), $subscription_manager->GetSubscription( $session_roster->GetAccountId() )->GetNbSpellsUpdated().$localization->Get('spell(s)'));
$list_table->AddLine($localization->Get('number of reagents updated'), $subscription_manager->GetSubscription( $session_roster->GetAccountId() )->GetNbReagentsUpdated().$localization->Get('reagent(s)'));
$list_table->AddLine($localization->Get('last upload'), $subscription_manager->GetSubscription( $session_roster->GetAccountId() )->GetLastUpload());

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
	<head>
		<title>LZRoster<?php echo $config->HasRosterTitle() ?  " - ".$config->GetRosterTitle() : null; ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/_.css" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/preferences.css" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/listtable.css" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/wowtable.css" />
		<script type="text/javascript" src="js/overlib.js"><!-- overLIB (c) Erik Bosrup --></script>
		<script type="text/javascript" src="js/display.js"></script>
	</head>
	<body>
		
		<?php require_once "inc/head.php"; ?>
		<?php require_once "inc/hmenu.php"; ?>
		
		<h2><?php echo $localization->Get('preferences'); ?></h2>
		
		<?php
		if ($status_roster->HasMessage() or $session_roster->HasStatus())
		{
		?>
			
			<p>
				<?php echo $localization->Get('this page produced some messages. please refer to the following lines'); ?>
			</p>
			
			<?php echo $session_roster->HasStatus() ? $session_roster->GetStatus()->GetStack_InList() : $status_roster->GetStack_InList(); ?>
			
			<p>
				<?php echo $localization->Get('you can refresh the page %s and try again', "preferences.php"); ?>
			</p>
			
		<?php
		}
		else
		{
		?>
		
		<p id="account">
			<strong><?php echo $subscription_manager->GetSubscription( $session_roster->GetAccountId() )->GetNickName(); ?></strong><br />
			<span><?php echo $localization->Get('subscription'); ?> <?php echo $subscription_manager->GetSubscription( $session_roster->GetAccountId() )->GetCreationDate(); ?></span>
		</p>
		
		<?php echo $list_table->GetTable(); ?>
		
		<h3><?php echo $localization->Get('characters bound to this account'); ?></h3>
		
		<p>
			<?php echo $localization->Get('you an manage your characters by clicking (...)'); ?>
		</p>
		
		<?php $wow_table->GetAndPrintTable(); ?>
		
		<h3><?php echo $localization->Get('change the language of the roster'); ?></h3>
		<p>
			<?php echo $localization->Get('current language'); ?> <a href="change_language.php?from=preferences.php"><img src="img/preferences/change_language_off.png" alt="" onmouseover="this.src='img/preferences/change_language_on.png';" onmouseout="this.src='img/preferences/change_language_off.png';" style="vertical-align: middle;" id="img_chg_lang" /><strong style="color: black; margin-left: -120px;" onmouseover="document.getElementById('img_chg_lang').src='img/preferences/change_language_on.png';" onmouseout="document.getElementById('img_chg_lang').src='img/preferences/change_language_off.png';" ><?php echo $locale_container->GetLocale($session_roster->GetLocalization())->GetName(); ?></strong></a>
		</p>
		<p>
			<?php echo $localization->Get('to change the language of the roster, simply click (...)'); ?>
		</p>
		
		
		<div style="float: left;">
			<h3><?php echo $localization->Get('change your password'); ?></h3>
			<form action="preferences.php" method="post" class="big_form">
			<p>
				<label for="oldpass">
					<?php echo $localization->Get('old password'); ?>
				</label>
				<input
					type="password"
					name="oldpass"
					id="oldpass"
				/>
			</p>
			<p>
				<label for="newpass">
					<?php echo $localization->Get('new password'); ?>
				</label>
				<input
					type="password"
					name="newpass"
					id="newpass"
				/><br />
				
				<label for="confirmpass">
					<?php echo $localization->Get('confirm new password'); ?>
				</label>
				<input
					type="password"
					name="confirmpass"
					id="confirmpass"
				/>
			</p>
			
			<p class="submit">
				<input type="submit" name="c_password" value="<?php echo $localization->Get('change'); ?>" />
			</p>
			</form>
		</div>
			
		<div style="float: right;">
			<h3><?php echo $localization->Get('delete account'); ?></h3>
			<form action="preferences.php" method="post" class="little_form">
			<p>
				<span><?php echo $localization->Get('password'); ?>:</span>
				<input type="password" name="pass" />
			</p>
			<p class="submit">
				<input type="submit" name="del_account" value="<?php echo $localization->Get('delete'); ?>" onclick="return confirm('<?php echo $tooltip->Flatten( $localization->Get('do you really want to delete this acocunt from the roster? all related data (...)')); ?>');" />
			</p>
			</form>
		</div>
		
		<?php
		}
		?>
		
		<?php require_once "inc/foot.php"; ?>
		
	</body>
</html>